package priv.ln.project.service;

import priv.ln.project.domain.Architect;
import priv.ln.project.domain.Designer;
import priv.ln.project.domain.Employee;
import priv.ln.project.domain.Programmer;

/**
 * ClassName: TeamService
 * Package: priv.ln.project.service
 * Description:
 * 关于开发团队成员的管理：添加、删除等
 *
 * @Author 辣辣
 * @Create 2024/7/17 20:47
 * @Version 1.0
 */
public class TeamService {
    private static int counter = 1;//为开发团队新增成员自动生成团队中的唯一ID，即memberId。
    private final int MAX_MEMBER = 5;//开发团队最大成员数
    private Programmer[] team = new Programmer[MAX_MEMBER];//保存当前团队中的各成员对象
    private int total = 0;//记录团队成员的实际人数

    /**
     * 返回当前团队的所有对象
     *
     * @return 包含所有成员对象的数组，数组大小与成员人数一致
     */
    public Programmer[] getTeam() {
        Programmer[] currentTeam = new Programmer[total];
        for (int i = 0; i < total; i++) {
            currentTeam[i] = team[i];
        }
        return currentTeam;
    }

    /**
     * 向团队中添加成员
     *
     * @param e 待添加成员的对象
     * @throws TeamException 添加失败， TeamException中包含了失败原因
     */
    public void addMember(Employee e) throws TeamException {

        if (total == MAX_MEMBER) {
            throw new TeamException("成员已满，无法添加");
        }
        if (e instanceof Programmer) {
            int programmerNumber = 0;
            int designerNumber = 0;
            int architectNumber = 0;
            Programmer programmer = (Programmer) e;
            if (programmer.getMemberId() != 0) {
                throw new TeamException("该员工已是某团队成员");
            }
            if (programmer.getStatus() == Status.VOCATION){
                throw new TeamException("该员工正在休假，无法添加");
            }

            for (int i = 0; i < total; i++) {
                if(team[i] instanceof Architect){
                    architectNumber++;
                }else if(team[i] instanceof Designer){
                    designerNumber++;
                }else {
                    programmerNumber++;
                }
            }

            if(e instanceof Architect) {
                if(architectNumber == 1){
                    throw new TeamException("团队中至多只能有一名架构师");
                }
            }else if(e instanceof Designer){
                if(designerNumber == 2) {
                    throw new TeamException("团队中至多只能有两名设计师");
                }
            }else if(programmerNumber == 3){
                throw new TeamException("团队中至多只能有三名程序员");
            }

            team[total] = programmer;
            programmer.setMemberId(counter);
            programmer.setStatus(Status.BUSY);
            counter++;
            total++;
        } else throw new TeamException("该成员不是开发人员");
    }

    /**
     * 从团队中删除成员
     *
     * @param memberId 待删除成员的memberId
     * @throws TeamException 找不到指定memberId的员工，删除失败
     */
    public void removeMember(int memberId) throws TeamException {
        for (int i = 0; i < total; i++) {
            if (team[i].getMemberId() == memberId) {
                team[i].setStatus(Status.FREE);
                for (int j = i; j < total - 1; j++) {
                    team[j] = team[j + 1];
                }
                team[total-1] = null;
                total--;
                return;
            }
        }
        throw new TeamException("找不到指定memberId的员工，删除失败");
    }


}
